package com.wwz.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.wwz.CustomCellWriteHandler;
import com.wwz.DemoData;
import com.wwz.ScheduleExportHandler;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Test;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class excelUtil {

    private <T> void export(HttpServletResponse response, String fileName, List<T> data, Class<T> clz) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileNameEncode = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileNameEncode + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clz)
                    .sheet("Sheet1")
                    .doWrite(data);
        } catch (Exception e) {

        }
    }

    @Test
    public void simpleWrite() throws InterruptedException, ParseException {
        //String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        LocalDate localDate = LocalDate.now();
        String fileName = "排期查询-" + localDate.toString().replace("-", ".")+ ".xlsx";
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        List<DemoData> data = data();
        EasyExcel.write(fileName, DemoData.class)
                .sheet("模板")
                .head(head(data))
                .registerWriteHandler(new CustomCellWriteHandler())
                .registerWriteHandler(horizontalCellStyleStrategy)
                .registerWriteHandler(new ScheduleExportHandler())
                .doWrite(data);
    }


    private List<DemoData> data() throws InterruptedException {
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            if(i==0){
                data.setString("开机屏" + i+"D");
            }else {
                data.setString("开机屏" + i);
            }
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
            Thread.sleep(1000);
        }
        return list;
    }

    private List<List<String>> head(List<DemoData> data) throws ParseException {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("备注");
        head0.add("资源位置");
        list.add(head0);
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0; i < data.size(); i++) {
            List<String> head = new ArrayList<String>();
            head.add("1、深色填充表示当天所有库存已售。2、表格为空表示当天全部库存可售。3、表格内容为当天售卖或预占信息。");
            head.add(data.get(i).getDate().toString());
            head.add(dateToWeek(format.parse("20201217")));
            list.add(head);
        }

        return list;
    }

    public String dateToWeek(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


}
